package com.tdam2014.grupo3.adapters;

import java.util.ArrayList;

import android.content.Context;
import android.content.res.Resources;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.tdam2014.grupo3.R;
import com.tdam2014.grupo3.entities.PhotoSet;

public class PhotoSetAdapter extends BaseAdapter {
	
	private LayoutInflater inflater;
	private ArrayList<PhotoSet> photoSets;
	private Resources res;
	
	public PhotoSetAdapter(Context context) {
		this.inflater = LayoutInflater.from(context);
		this.res = context.getResources();
	}

	public void setPhotoSets(ArrayList<PhotoSet> photoSets) {
		this.photoSets = photoSets;
	}

	@Override
	public int getCount() {
		return photoSets.size();
	}

	@Override
	public PhotoSet getItem(int position) {
		return photoSets.get(position);
	}

	@Override
	public long getItemId(int position) {
		return -1;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		View item = null;
		HolderPhotoSetItemList holder = null;
		
		if(convertView == null) {
			item = inflater.inflate(R.layout.photo_set_list_item, null);
			holder = new HolderPhotoSetItemList();
			holder.title = (TextView) item.findViewById(R.id.txtDirectoryTitle);
			holder.data = (TextView) item.findViewById(R.id.txtDirectoryData);
			item.setTag(holder);
		} else {
			item = convertView;
			holder = (HolderPhotoSetItemList) item.getTag();
		}
		
		PhotoSet photoSet = photoSets.get(position);
		
		holder.title.setText(photoSet.getTitle());
		if(photoSet.getCountPhotos() == 1) holder.data.setText(res.getString(R.string.created_at) + " " + photoSet.getDateCreatedDate() + " | " + photoSet.getCountPhotos() + " " + res.getString(R.string.photo));
		else holder.data.setText(res.getString(R.string.created_at) + " " + photoSet.getDateCreatedDate() + " | " + photoSet.getCountPhotos() + " " + res.getString(R.string.photos));
		
		return item;
	}
	
	private class HolderPhotoSetItemList {
		TextView title;
		TextView data;
	}
}
